package com.weipai.demo.controller;

import com.weipai.demo.enums.RetCode;
import com.weipai.demo.model.LoansManager;
import com.weipai.demo.model.ManagerProduct;
import com.weipai.demo.model.Rate;
import com.weipai.demo.service.FileUploadService;
import com.weipai.demo.service.LoansManagerService;
import com.weipai.demo.util.LayuiTablePagingResult;
import com.weipai.demo.util.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.text.DecimalFormat;
import java.util.ArrayList;

/**
 * @Author: 赵项阳
 * @Date: 2019/6/29 18:21
 * @Version 1.0
 */
@RestController
public class LoansManagerController {


    @Autowired
    private LoansManagerService loansManagerService;
    @Autowired
    private FileUploadService fileUploadService;


      //后台管理页面
    //==================================================================================================================

    //信贷经理注册信息
    @RequestMapping(value = "/manager/register")
    public Response register(LoansManager LoansManager){

        return loansManagerService. register(LoansManager);
    }


    //分页展示信贷经理信息
    @RequestMapping(value = "/manager/managerShow")
    public LayuiTablePagingResult managerShow(Integer page,Integer size,Integer status){

        return loansManagerService.managerShow(page,size,status);
    }

    //审核通过和驳回或删除
    @RequestMapping(value = "/manager/isStatus")
    public Response isStatus(Integer id,Integer status){
     return loansManagerService.isStatus(id,status);
    }

    //修改为已付款状态
    @RequestMapping(value = "/manager/changePay")
    public Response changePay(Integer id){
     return loansManagerService.changePay(id);
    }


    //展示
    @RequestMapping(value = "/manager/showKey")
    public Response showKey(Integer status){
        return loansManagerService.showKey(status);
    }




      //前台信贷经理页面
    //==================================================================================================================

    //页面展示信贷经理信息
    @RequestMapping(value = "/front/show")
    public Response show(String phone){
        return loansManagerService.show(phone);

    }



    //信贷经理头像图片上传
    @RequestMapping(value = "/front/uploadingImg", method = RequestMethod.POST)
    public Response uploadingImg(HttpServletRequest request, LoansManager loansManager, MultipartFile img) {
        Response response = new Response();
        if (img != null) {
            String result = fileUploadService.saveImgUpload(request, img);
            if (result.equals("formatError")) {
                response.setCode(RetCode.IMG_FORMAT);
            } else if (result.equals("sizeover")) {
                response.setCode(RetCode.IMG_SIZE);
            } else if (result.equals("error")) {
                response.setCode(RetCode.IMG_ERROR);
            } else {
                loansManager.setManagerPictureUrl(result);
                response.put("result",result);
                response.setCode(RetCode.SUCCESS);
               // response = loansManagerService.savePicture(loansManager);
            }
        }else {
            loansManager.setManagerPictureUrl(loansManager.getManagerPictureUrl());
            response = loansManagerService.savePicture(loansManager);
        }
        return response;

    }



    //页面信贷经理创建贷款产品
    @RequestMapping(value = "/front/saveProduct",method = RequestMethod.POST)
    public Response saveProduct(ManagerProduct managerProduct){
        return loansManagerService.saveProduct(managerProduct);
    }


    //信贷经理产品展示
    @RequestMapping(value = "/front/showPage",method = RequestMethod.POST)
    public LayuiTablePagingResult showPage(Integer page,Integer size,String status,String managerPhone){
        return loansManagerService.showPage(page,size,status,managerPhone);
    }


    //信贷经理查看详情
    @RequestMapping(value = "/front/particulars")
    public Response particulars(Integer id){
        return loansManagerService.particulars(id);
    }

    //上下架
    @RequestMapping(value = "/front/upOrDown")
    public Response upOrDown(Integer id,String status){
        return loansManagerService.upOrDown(id,status);
    }

    //删除产品
    @RequestMapping(value = "/front/deleteProduct")
    public Response deleteProduct(Integer id){
        return loansManagerService.deleteProduct(id);
    }

    //页面信贷经理信息修改
    @RequestMapping(value = "/front/change")
    public Response change(LoansManager loansManager){
        return loansManagerService.change(loansManager);
    }

    //热门贷款
    @RequestMapping(value = "/front/recommendList")
    public Response recommendList(Integer page,Integer size){
        return loansManagerService.recommendList(page,size);
    }





    //信贷经理页面搜索贷款
    @RequestMapping(value = "/front/managerSearch")
    public LayuiTablePagingResult managerSearch(Integer page, Integer size, String productName, String status, String createTime,String createTimes,Integer publishStatus,String managerPhone){
     return loansManagerService.managerSearch(page,size,productName,status,createTime,createTimes,publishStatus,managerPhone);
    }





    /**
     * 计算等额本息还款
     *
     * @param principal 贷款总额
     * @param months    贷款期限
     * @param rate      贷款利率
     * @return
     */

    @RequestMapping(value = "/front/jisuan")
    public Response jisuan (double principal, int months, double rate) {
        Response response=new Response();
        ArrayList<String> data = new ArrayList<String>();
        Rate rt = new Rate();
        months=months*12;
        double monthRate = rate / (principal * 12);//月利率
        double preLoan = (principal * monthRate * Math.pow((1 + monthRate), months)) / (Math.pow((1 + monthRate), months) - 1);//每月还款金额
        double totalMoney = preLoan * months;//还款总额
        double interest = totalMoney - principal;//还款总利息
        DecimalFormat df=new DecimalFormat("#.###");
        df.format(totalMoney);
        df.format(interest);
        df.format(preLoan);


      /*  rt.setTotalMoney(Double.parseDouble(df.format(totalMoney)));//还款总额
        rt.setPreLoan(Double.parseDouble(df.format(preLoan)));//每月还款金额
        rt.setMonths(months);//还款期限
        rt.setPrincipal(principal);//贷款总额
        rt.setInterest(Double.parseDouble(df.format(interest)));//还款总利息
        response.put("rt",rt);*/
        data.add( "还款总额:"+df.format(totalMoney)+"万");//还款总额
        data.add("贷款总额:"+String.valueOf(principal)+"万");//贷款总额
        data.add("还款总利息:"+df.format(interest)+"万");//还款总利息
        data.add("每月还款金额:"+df.format(preLoan*10000)+"元");//每月还款金额
        data.add("还款期限:"+String.valueOf(months)+"个月");//还款期限
       // return data.toArray(new String[data.size()]);
        response.put("jisuan",data.toArray(new String[data.size()]));
        response.setCode(RetCode.SUCCESS);
        return response;
    }

    //搜索贷款
    @RequestMapping(value = "/front/search")
    public LayuiTablePagingResult search (Integer page,Integer size,ManagerProduct managerProduct){
//        Integer scopeId,Integer loanPeriodId,
//                String applicantId,Integer housePropertyId,
//                Integer isVehicleId,Integer isCreditId,
//                Integer institutionalTypeId,Integer pledgeMethodId,
//                Integer modeOfRepaymentId,
//        ,scopeId,loanPeriodId,applicantId,housePropertyId,institutionalTypeId,isVehicleId,isCreditId,pledgeMethodId,modeOfRepaymentId
        return loansManagerService.search(page,size,managerProduct);
    }


    //页面按照月供或总利息查询desc
    @RequestMapping(value = "/front/interestAndSupply")
    public LayuiTablePagingResult interestAndSupply(String totleFee,String monthlySupply,Integer page,Integer size){
      return loansManagerService.interestAndSupply(page,size,totleFee,monthlySupply);
    }

    //贷款详情
    @RequestMapping(value = "/front/getDetail")
    public Response getDetail(Integer id){
      return loansManagerService.getDetail(id);
    }

    //贷款信息上下架
    @RequestMapping( value = "/fornt/changeStatus")
    public Response changeStatus(Integer id,Integer publishStatus){
        return loansManagerService.changeStatus(id,publishStatus);
    }

    @RequestMapping(value = "/fornt/getOrder")
    public Response getOrder(String phone){
        return loansManagerService.getOrder(phone);
    }


































}
